﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Huirui.Cavan.Core.Extension;
using AutoMapper;
using Newtonsoft.Json;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class SupplierTypeController : EntityController<SupplierType, int, SupplierTypeSearch, ISupplierTypeViewModel, ISupplierTypeAppService>, ISupplierTypeController
    {
        public override string ControllerName
        {
            get { return ControllerConst.SUPPLIERTYPE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public SupplierTypeController()
        {
            this.Model = ModelFactory.CreateModel<ISupplierTypeViewModel>();
        }

        [ActionName(ActionConst.LIST)]
        [HttpPost]
        public ActionResult GetSupplierType()
        {
            SupplierTypeJsonViewModel json = new SupplierTypeJsonViewModel();
            json.rows = new List<SupplierTypeJson>();
            IList<SupplierType> suppliertypes = this.AppService.GetBySearch(SearchFactory.CreateSupplierTypeSearch()).ToList();
            foreach (SupplierType s in suppliertypes)
            {
                SupplierTypeJson sj = Mapper.Map<SupplierType, SupplierTypeJson>(s);
                json.rows.Add(sj);
            }
            json.total = suppliertypes.Count;
            return Json(json, "application/json",JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        public ActionResult GetSupplierType(string suppliertypename)
        {
            SupplierTypeJsonViewModel json = new SupplierTypeJsonViewModel();
            json.rows = new List<SupplierTypeJson>();
            IList<SupplierType> suppliertypes = this.AppService.GetBySearch(SearchFactory.CreateSupplierTypeSearch(suppliertypename)).ToList();
            foreach (SupplierType s in suppliertypes)
            {
                SupplierTypeJson sj = Mapper.Map<SupplierType, SupplierTypeJson>(s);
                json.rows.Add(sj);
            }
            json.total = suppliertypes.Count;
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }


        [ActionName(ActionConst.ADD)]
        [HttpGet]
        public ActionResult AddSupplierType()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            this.Model.CmbName = RouteData.Values["cmbname"].ToString();
            return PartialView("~/Views/System/v1/partial/addSupplierTypePartial.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        public ActionResult AddSupplierType(SupplierTypeViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.ModifyDate = DateTime.Now;
            model.Item.CreateDate = DateTime.Now;
            bool isExist = (this.AppService.GetBySearch(SearchFactory.CreateSupplierTypeSearch(model.Item.Name)).Count() > 0);
            BoolGenericResult<SupplierType> result = null; ;
            if (!isExist)
            {
                result = this.AppService.AddOrUpdate(model.Item);
                result.Messages.Add(result.Object.Id.ToString());
                result.Messages.Add(result.Object.Name.ToString());
            }
            else
            {
                result = new BoolGenericResult<SupplierType>();
                result.IsSuccess = false;
                result.Messages = new List<string> { string.Format("{0}已经存在", model.Item.Name) };
            }
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            return Json(jsonResult, "application/json");


        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        public JsonResult DeleteSupplierType(string selected)
        {
            BoolGenericResult<SupplierType> result = null;
            List<SupplierTypeJson> suptypes = JsonConvert.DeserializeObject<List<SupplierTypeJson>>(selected);

            foreach (SupplierTypeJson json in suptypes)
            {
                SupplierType ai = this.AppService.GetById(int.Parse(json.Id));
                result = this.AppService.Delete(ai);
                result.Messages.Add(result.Object.Id.ToString());
                result.Messages.Add(result.Object.Name.ToString());
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

    }
}
